package com.liu.algorithm.search.twoPointer;


/**
 * 给定一个字符串，你需要反转字符串中每个单词的字符顺序，同时仍保留空格和单词的初始顺序。
 */
public class Leetcode557 {
    public static void main(String[] args) {
        Leetcode557 obj = new Leetcode557();
        String s = "Let's take LeetCode contest";
        System.out.println(obj.reverseWords(s));
    }

    public String reverseWords(String s) {
        String[] arr = s.split(" ");
        String result = "";
        for (int i = 0; i < arr.length; i++) {
            result += swap(arr[i]);
            if (i != arr.length - 1) {
                result += " ";
            }
        }
        return result;
    }

    public String swap(String s) {
        String result = "";
        char[] chars = s.toCharArray();
        for (int left = 0, right = chars.length - 1; left < right; left++, right--) {
            char temp = chars[left];
            chars[left] = chars[right];
            chars[right] = temp;
        }
        for (int i = 0; i < chars.length; i++) {
            result += chars[i];
        }
        return result;
    }
}
